package com.tengfei.blogsystem.utils;

import com.tengfei.blogsystem.pojo.User;
import io.jsonwebtoken.Claims;

import java.util.HashMap;
import java.util.Map;

/**
 * @version v1.0
 * @author} feige
 * @date 2021-10-18-15:48
 * @description
 */
public class ClaimsUtils {

    /**
     * @param user
     * @return
     */
    public static Map<String, Object> UserToClaims(User user) {
        Map<String, Object> claims = new HashMap<>();
        claims.put("id", user.getId());
        claims.put("user_name", user.getUserName());
        claims.put("roles", user.getRoles());
        claims.put("avatar", user.getAvatar());
        claims.put("email", user.getEmail());
        claims.put("sign", user.getSign());
        return claims;
    }

    /**
     * @param claims
     * @return
     */
    public static User ClaimsToUser(Claims claims) {
        User user = new User();
        String id = (String) claims.get("id");
        user.setId(id);
        String userName = (String) claims.get("user_name");
        user.setUserName(userName);
        String roles = (String) claims.get("roles");
        user.setRoles(roles);
        String avatar = (String) claims.get("avatar");
        user.setAvatar(avatar);
        String email = (String) claims.get("email");
        user.setEmail(email);
        String sign = (String) claims.get("sign");
        user.setSign(sign);
        return user;
    }
}
